//
//  ViewController.m
//  Misaligned_Images
//
//  Created by lirenqiang on 2016/11/3.
//  Copyright © 2016年 1111. All rights reserved.
//

#import "ViewController.h"
static NSString *picUrl = @"http://assets.barcroftmedia.com.s3-website-eu-west-1.amazonaws.com/assets/images/recent-images-11.jpg";
typedef void(^assignment)(UIImage *image);

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *img;
@property (weak, nonatomic) IBOutlet UIImageView *img2;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:picUrl]];
//    __block UIImage *image;
//    [[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
//        image = [UIImage imageWithData:data];
//        NSLog(@"%@", [NSThread currentThread]);
//        
//        dispatch_async(dispatch_get_main_queue(), ^{
//           self.img2.image = image;
//        });
//        
//        [self drawImageWithSize:self.img.frame.size image:image ComplitionHandler:^(UIImage *image) {
//            self.img.image = image;
//        }];
//    }] resume];
    
    
    UIButton *btn = [UIButton new];
    [btn setTitle:@"你好啊" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    btn.frame = CGRectMake(100, 100, 100, 100);
    [self.view addSubview:btn];
    btn.titleLabel.font = [UIFont systemFontOfSize:12];
    UIButton *btn1 = [UIButton new];
    [btn1 setTitle:@"你好啊" forState:UIControlStateNormal];
    [btn1 sizeToFit];
    btn1.center = CGPointMake(50, 50);
    [btn1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btn1.titleLabel.font = [UIFont systemFontOfSize:12];
    [self.view addSubview:btn1];
    NSString *str = @"你好啊";
    CGRect rect = [str boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName :[UIFont systemFontOfSize:12]} context:nil];
    NSLog(@"rect : %@", NSStringFromCGRect(rect));
    UILabel *label = [UILabel new];
    label.text = @"你好啊";
    label.font = [UIFont systemFontOfSize:12];
    [label sizeToFit];
    NSLog(@"%@", NSStringFromCGRect(label.frame));
//    btn1.frame = CGRectMake(100, 100, rect.size.width, rect.size.height);
    btn.frame = CGRectMake(200, 200, label.frame.size.width, label.frame.size.height);
}


- (void)drawImageWithSize:(CGSize)size image:(UIImage *)image ComplitionHandler:(assignment)finished
{
    UIGraphicsBeginImageContextWithOptions(size, YES, 0);
    
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
    
    UIImage * final = UIGraphicsGetImageFromCurrentImageContext();
    
    dispatch_async(dispatch_get_main_queue(), ^{
        
        finished(final);
    });
    
    UIGraphicsEndImageContext();
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
